package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.service.ISysFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 系统文件模块
 *
 * @author syw
 * @since 2023-06-24
 **/
@RestController
@RequestMapping("/file")
@Api(tags = "系统文件模块")
@Slf4j
@RequiredArgsConstructor
public class SysFileController {

    private final ISysFileService sysFileService;

    @Value("${upload.file.path}")
    private String uploadPath;
    @Value("${upload.file.register.license}")
    private String uploadLicensePath;
    @Value("${upload.file.register.auth}")
    private String uploadAuthPath;
    @Value("${upload.file.temp}")
    private String uploadTempPath;

    @ApiOperation(value = "上传文件")
    @PostMapping(value = "/upload")
    //@ApiImplicitParam(name = "file", value = "上传的文件", dataType = "_File", dataTypeClass = MultipartFile.class, required = true)
    public Response<String> fileUpload(@RequestPart(value = "file") MultipartFile file) {
        return ResponseFactory.okResponse(sysFileService.fileUpload(file, uploadPath));
    }


    @ApiOperation(value = "上传临时文件")
    @PostMapping(value = "/upload/temp")
    //@ApiImplicitParam(name = "file", value = "上传的文件", dataType = "_File", dataTypeClass = MultipartFile.class, required = true)
    public Response<String> fileUploadTemp(@RequestPart(value = "file") MultipartFile file) {
        return ResponseFactory.okResponse(sysFileService.fileUpload(file, uploadTempPath));
    }

    @ApiOperation(value = "上传文件")
    @PostMapping(value = "/upload/info")
    public Response<String> fileInfoUpload(@RequestPart(value = "file") MultipartFile file) {
        return ResponseFactory.okResponse(sysFileService.fileUpload(file, uploadAuthPath));
    }

    @ApiOperation(value = "下载文件")
    @GetMapping("/download")
    public Response<Boolean> fileDownload(HttpServletResponse response, @RequestParam String url) {
        return ResponseFactory.okResponse(sysFileService.fileDownload(response, url));
    }

    @ApiOperation(value = "上传营业执照")
    @PostMapping(value = "/upload/register-license")
    // @ApiImplicitParam(name = "file", value = "上传的文件", dataType = "_File", dataTypeClass = MultipartFile.class, required = true)
    public Response<String> uploadRegisterLicenseFile(@RequestPart(value = "file") MultipartFile file) {
        return ResponseFactory.okResponse(sysFileService.fileUpload(file, uploadLicensePath));
    }

    @ApiOperation(value = "上传注册授权证书")
    @PostMapping(value = "/upload/register-auth")
    //@ApiImplicitParam(name = "file", value = "上传的文件", dataType = "_File", dataTypeClass = MultipartFile.class, required = true)
    public Response<String> uploadRegisterAuthFile(@RequestPart(value = "file") MultipartFile file) {
        return ResponseFactory.okResponse(sysFileService.fileUpload(file, uploadAuthPath));
    }

    @ApiOperation(value = "获取地图Json")
    @GetMapping("/download/map-json")
    public Response<String> downloadMapJson() {
        return ResponseFactory.okResponse(sysFileService.downloadMapJson());
    }
}
